package com.example.as;


import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{


    //创建对象
    private Fragment_caidan caidanFragment=new Fragment_caidan();
    private Fragment_meiti meitiFragment=new Fragment_meiti();
    private Fragment_tonghua tonghuaFragment=new Fragment_tonghua();
    private Fragment_zhongxin zhongxinFragment=new Fragment_zhongxin();
    private FragmentManager fragmentManager;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;

    private ImageView image1,image2,image3,image4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        linearLayout1=findViewById(R.id.LinearLayout_caidan);
        linearLayout2=findViewById(R.id.LinearLayout_meiti);
        linearLayout3=findViewById(R.id.LinearLayout_tonghua);
        linearLayout4=findViewById(R.id.LinearLayout_zhongxin);

        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);

        image1=findViewById(R.id.imageView1);
        image2=findViewById(R.id.imageView2);
        image3=findViewById(R.id.imageView3);
        image4=findViewById(R.id.imageView4);

        initFragment();
        selectFragment(0);

    }

    //初始化
    private void initFragment(){
        fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.id_content,caidanFragment);
        transaction.add(R.id.id_content,meitiFragment);
        transaction.add(R.id.id_content,tonghuaFragment);
        transaction.add(R.id.id_content,zhongxinFragment);
        transaction.commit();
    }
    //隐藏
    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(caidanFragment);
        transaction.hide(meitiFragment);
        transaction.hide(tonghuaFragment);
        transaction.hide(zhongxinFragment);
    }
    private void hideColor(){
        image1.setColorFilter(Color.WHITE);
        image2.setColorFilter(Color.WHITE);
        image3.setColorFilter(Color.WHITE);
        image4.setColorFilter(Color.WHITE);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.LinearLayout_caidan:
                selectFragment(0);
                hideColor();
                image1.setColorFilter(Color.BLUE);
                break;
            case R.id.LinearLayout_zhongxin:
                selectFragment(1);
                hideColor();
                image2.setColorFilter(Color.GREEN);
                break;
            case R.id.LinearLayout_meiti:
                selectFragment(2);
                hideColor();
                image3.setColorFilter(Color.BLUE);
                break;
            case R.id.LinearLayout_tonghua:
                selectFragment(3);
                hideColor();
                image4.setColorFilter(Color.GREEN);
                break;
            default:
                break;
        }
    }

    private void selectFragment(int i){
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i){
            case 0:
                transaction.show(caidanFragment);
                break;
            case 1:
                transaction.show(zhongxinFragment);
                break;
            case 2:
                transaction.show(meitiFragment);
                break;
            case 3:
                transaction.show(tonghuaFragment);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}